@echo off
title Build Script for Login-Test Project
chcp 65001 >nul 2>&1

echo ========================================
echo Login-Test Project Build Script
echo ========================================
echo.

echo [Step 1] Setting up Python path...
set PYTHON_CMD=python
echo Python command set to: %PYTHON_CMD%

echo.
echo [Step 2] Checking Python installation...
echo Executing: %PYTHON_CMD% --version
%PYTHON_CMD% --version >nul 2>&1
if errorlevel 1 (
    echo.
    echo [ERROR] Python check failed, error code: %errorlevel%
    echo [INFO] Possible issues:
    echo   1. Python not installed
    echo   2. Python not added to PATH environment variable
    echo   3. Python command is not 'python', might be 'py' or 'python3'
    echo.
    echo [SOLUTION] Please try these steps:
    echo   1. Install Python: https://python.org
    echo   2. Make sure to check "Add to PATH" option
    echo   3. Restart command prompt
    echo.
    pause
    exit /b 1
)

echo [SUCCESS] Python check passed
echo.

echo [Step 3] Checking required files...
if not exist "login.py" (
    echo [ERROR] login.py file not found
    echo [Current Directory] %CD%
    dir *.py
    pause
    exit /b 1
)
echo [FOUND] login.py

if not exist "open.py" (
    echo [ERROR] open.py file not found
    pause
    exit /b 1
)
echo [FOUND] open.py

if not exist "requirements.txt" (
    echo [ERROR] requirements.txt file not found
    pause
    exit /b 1
)
echo [FOUND] requirements.txt

if not exist "config.py" (
    echo [WARNING] config.py file not found, creating basic version
    echo # Configuration file > config.py
    echo USERNAME = "" >> config.py
    echo PASSWORD = "" >> config.py
)
echo [FOUND] config.py

echo.
echo [Step 4] Upgrading pip...
echo Executing: %PYTHON_CMD% -m pip install --upgrade pip
%PYTHON_CMD% -m pip install --upgrade pip
if errorlevel 1 (
    echo [WARNING] pip upgrade failed, error code: %errorlevel%, continuing...
) else (
    echo [SUCCESS] pip upgrade completed
)

echo.
echo [Step 5] Installing dependencies...
echo Reading requirements.txt:
type requirements.txt
echo.
echo Executing: %PYTHON_CMD% -m pip install -r requirements.txt
%PYTHON_CMD% -m pip install -r requirements.txt
if errorlevel 1 (
    echo [ERROR] Dependencies installation failed, error code: %errorlevel%
    echo [INFO] Please check network connection and requirements.txt file content
    pause
    exit /b 1
)
echo [SUCCESS] Dependencies installation completed

echo.
echo [Step 6] Installing PyInstaller...
echo Executing: %PYTHON_CMD% -m pip install pyinstaller
%PYTHON_CMD% -m pip install pyinstaller
if errorlevel 1 (
    echo [ERROR] PyInstaller installation failed, error code: %errorlevel%
    pause
    exit /b 1
)
echo [SUCCESS] PyInstaller installation completed

echo.
echo [Step 7] Installing Playwright browsers (Chrome only)...
echo Executing: %PYTHON_CMD% -m playwright install chromium --force
%PYTHON_CMD% -m playwright install chromium --force
if errorlevel 1 (
    echo [ERROR] Playwright browser installation failed, error code: %errorlevel%
    echo [INFO] This might take a long time, please ensure network connection is stable
    pause
    exit /b 1
)
echo [SUCCESS] Playwright browser installation completed

echo.
echo [Step 8] Getting Playwright path...
echo Executing command to get Playwright path...
for /f "tokens=*" %%i in ('%PYTHON_CMD% -c "import playwright; import os; print(os.path.dirname(playwright.__file__))"') do set PLAYWRIGHT_PATH=%%i
if defined PLAYWRIGHT_PATH (
    echo [SUCCESS] Playwright path: %PLAYWRIGHT_PATH%
) else (
    echo [ERROR] Cannot get Playwright path
    pause
    exit /b 1
)

REM Check browser path
set BROWSER_PATH=%USERPROFILE%\AppData\Local\ms-playwright
echo Browser path: %BROWSER_PATH%
if not exist "%BROWSER_PATH%" (
    echo [ERROR] Playwright browser directory does not exist: %BROWSER_PATH%
    echo [INFO] Please re-run: %PYTHON_CMD% -m playwright install chromium
    pause
    exit /b 1
)
echo [SUCCESS] Browser path verification passed

echo.
echo [Step 9] Cleaning old files...
if exist build (
    echo [CLEANUP] Removing build directory...
    rmdir /s /q build
)
if exist dist (
    echo [CLEANUP] Removing dist directory...
    rmdir /s /q dist
)
if exist *.spec (
    echo [CLEANUP] Removing spec files...
    del *.spec
)
echo [SUCCESS] Cleanup completed

echo.
echo [Step 10] Building login.py...
echo Executing PyInstaller command...
echo Command: pyinstaller --onefile --add-data="%BROWSER_PATH%\chromium-*;ms-playwright" --add-data="config.py;." --hidden-import="playwright" --hidden-import="playwright.sync_api" --hidden-import="requests" --hidden-import="oss2" --hidden-import="argparse" --name="login" --console --clean login.py

pyinstaller --onefile --add-data="%BROWSER_PATH%\chromium-*;ms-playwright" --add-data="config.py;." --hidden-import="playwright" --hidden-import="playwright.sync_api" --hidden-import="requests" --hidden-import="oss2" --hidden-import="argparse" --name="login" --console --clean login.py

if errorlevel 1 (
    echo [ERROR] login.py packaging failed, error code: %errorlevel%
    echo [INFO] Check detailed error information above
    pause
    exit /b 1
)
echo [SUCCESS] login.py packaging completed

echo.
echo [Step 11] Building open.py...
echo Executing PyInstaller command...
pyinstaller --onefile --add-data="%BROWSER_PATH%\chromium-*;ms-playwright" --add-data="config.py;." --hidden-import="playwright" --hidden-import="playwright.sync_api" --hidden-import="requests" --hidden-import="argparse" --name="open" --console --clean open.py

if errorlevel 1 (
    echo [ERROR] open.py packaging failed, error code: %errorlevel%
    pause
    exit /b 1
)
echo [SUCCESS] open.py packaging completed

echo.
echo [Step 12] Verifying build results...
if exist "dist\login.exe" (
    echo [VERIFIED] login.exe created successfully
    for %%A in ("dist\login.exe") do echo [SIZE] login.exe: %%~zA bytes
) else (
    echo [ERROR] login.exe not found
)

if exist "dist\open.exe" (
    echo [VERIFIED] open.exe created successfully  
    for %%A in ("dist\open.exe") do echo [SIZE] open.exe: %%~zA bytes
) else (
    echo [ERROR] open.exe not found
)

echo.
echo ========================================
echo Build Completed! (Chrome Optimized)
echo ========================================
echo Executable files location:
echo - %CD%\dist\login.exe
echo - %CD%\dist\open.exe
echo.
echo === Usage Instructions ===
echo 1. First login: dist\login.exe -u phone_number
echo 2. Open multiple browsers: dist\open.exe -u phone_number -c 3
echo ========================================
echo.

echo Do you want to test the packaged programs? (y/N): 
set /p test_choice=
if /i "%test_choice%"=="y" (
    echo.
    echo [TEST] Testing login.exe help information...
    if exist "dist\login.exe" (
        dist\login.exe --help
    )
    echo.
    echo [TEST] Testing open.exe help information...
    if exist "dist\open.exe" (
        dist\open.exe --help
    )
)

echo.
echo [COMPLETED] Script execution finished, press any key to exit...
pause >nul